<template>
  <div>
    <el-card>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>网站管理</el-breadcrumb-item>
        <el-breadcrumb-item>卡密管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-button type="primary addcamis" @click="AddCamisDialogVisible = true"
        >生成卡密</el-button
      >
      <el-table :data="camisList" style="width: 100%">
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="size" label="大小"> </el-table-column>
        <el-table-column prop="key" label="卡密"> </el-table-column>
        <el-table-column prop="createdAt" label="生成时间"> </el-table-column>
        <el-table-column label="使用者ID">
          <template slot-scope="scope">
            {{ scope.row.user_id || "NULL" }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#409EFF"
              inactive-color="#ddd"
              @change="edit_state(scope.row)"
              disabled
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="btnEdit btn" @click="delCaim(scope.row)">
              <i class="el-icon-delete-solid"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="findInfo.pagenum"
        :page-sizes="[5, 10]"
        :page-size="findInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="findInfo.total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改 -->
    <el-dialog
      title="修改"
      :visible.sync="AddCamisDialogVisible"
      width="50%"
      :before-close="AddCamishandleClose"
    >
      <el-form
        ref="AddCamisForm"
        :rules="AddCamisRules"
        :model="AddCamisForm"
        label-width="80px"
      >
        <el-form-item label="生成数量" prop="number">
          <el-input v-model.number="AddCamisForm.number"></el-input>
        </el-form-item>
        <el-form-item label="容量大小" prop="size">
          <el-input v-model.number="AddCamisForm.size"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="AddCamishandleClose">取 消</el-button>
          <el-button type="primary" @click="OnsubmitAddCamis">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      camisList: [],
      // 图片信息对象
      findInfo: {
        pagenum: 1,
        pagesize: 5,
        total: 0,
      },
      AddCamisForm: {
        number: "",
        size: "",
      },
      AddCamisRules: {
        number: [
          { required: true, message: "请输入生成数量", trigger: "blur" },
          { type: "number", message: "生成数量必须为数字值" },
        ],
        size: [
          { required: true, message: "请输入容量大小", trigger: "blur" },
          { type: "number", message: "容量大小必须为数字值" },
        ],
      },
      AddCamisDialogVisible: false,
    };
  },
  created() {
    this.getcamisList();
  },
  methods: {
    // 获取卡密列表
    async getcamisList() {
      const { data: res } = await this.$http.get("/camis", {
        params: this.findInfo,
      });
      this.findInfo.total = res.data.total;
      this.camisList = res.data.list;
      console.log(this.camisList);
    },
    // 添加卡密对话框 关闭 触发
    AddCamishandleClose() {
      this.$refs.AddCamisForm.resetFields();
      this.AddCamisDialogVisible = false;
    },
    // 确认生成卡密
    OnsubmitAddCamis() {
      this.$refs.AddCamisForm.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请正确的填写表单！");
          return;
        }
        const { data: res } = await this.$http.post(
          "/addCami",
          this.AddCamisForm
        );
        if (res.meta.status != 201) {
          this.$message.error(res.meta.msg);
          return;
        }
        this.getcamisList();
        this.AddCamisDialogVisible = false;
        this.$message.success("生成成功！");
      });
    },

    handleSizeChange(size) {
      this.findInfo.pagesize = size;
      this.getcamisList();
    },
    handleCurrentChange(page) {
      this.findInfo.pagenum = page;
      this.getcamisList();
    },
    // 删除卡密
    async delCaim(row) {
      this.$confirm("此操作将永久删除该卡密, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.post("/delcami", {
            id: row.id,
          });
          if (res.meta.status != 200) {
            this.$message.error(res.meta.msg);
            return;
          }
          this.$message.success("删除成功！");
          this.getcamisList();
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.btn {
  margin-left: 5px;
  width: 24px;
  height: 22px;
  text-align: center;
  line-height: 22px;
  border-radius: 3px;
  cursor: pointer;
}
.btn i {
  color: #fff;
}
.btnEdit {
  background: #f05050;
}
.addcamis {
  margin: 15px 0;
}
</style>
